package com.itheima.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/cache")
@CrossOrigin
public class Cache {//map缓存练习   因为导入了redis的依赖他自动存进redis了，没导入会存进map


    @Autowired
    private CacheManager cacheManager;


    @GetMapping
    @CachePut(value = "mz",key = "#result")
    public String save(String name){
        System.out.println("缓存名字成功");
        return name;
    }

    @GetMapping("/remove")
    @CacheEvict(value = "mz",key = "#name")
    public void  remove(String name){
        System.out.println("删除缓存成功");
    }

    @GetMapping("/getList")
    @Cacheable(value = "mz",key = "#name",unless = "#result == null")
    public String getList(String name){
        System.out.println("返回的不是缓存不出现此提示返回的是缓存");
        return name;
    }
}
